﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.MarketUI {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import flash.geom.*;
    import Events.*;
    import flash.utils.*;
    import org.aswing.*;
    import Classes.*;
    import Classes.util.*;
    import org.aswing.geom.*;
    import Display.*;
    import GameMode.*;
    import flash.text.*;
    import org.aswing.event.*;
    import com.zynga.skelly.util.color.*;
    import Display.aswingui.*;
    import L10n.*;
    import Display.MarketUI.assets.*;

    public class ItemCatalogUI extends JPanel implements ICatalogUI {

        protected const BIG_FONT_SIZE:Number = 22;

        protected var m_items:Array;
        protected var m_newItems:Array;
        protected var m_shelf:MarketScrollingList;
        protected var m_type:String;
        protected var m_catalog:Catalog;
        protected var m_itemName:String;
        protected var m_subTabsNames:Dictionary;
        protected var m_containerWidth:Number;
        protected var m_shelfAP:AssetPane;
        protected var m_tabsPanel:JPanel;
        protected var m_centerPanel:JPanel;
        protected var m_middlePanel:JPanel;
        protected var m_topPanel:JPanel;
        protected var m_topLeftPanel:JPanel;
        private var m_tabsArray:Array;
        protected var m_closeButton:JButton;
        protected var m_menuButton:JButton;
        protected var m_toolTip:ItemCatalogToolTip;
        protected var m_isInitialized:Boolean;
        protected var m_tabIndices:Dictionary;
        protected var m_exclusive:Boolean;
        protected var m_overrideTitle:String;
        private var m_tweenChildren:Dictionary;
        protected var m_tabsDict:Dictionary;
        protected var m_iconsDict:Dictionary;
        private var m_toolTipDict:Dictionary;
        protected var m_sectionTitle:JTextField;
        private var m_tabNames:Array;
        private var m_selectedTab:AssetPane;

        public function ItemCatalogUI(_arg1:Boolean=false){
            this.m_tabIndices = new Dictionary(true);
            this.m_tweenChildren = new Dictionary(true);
            this.m_tabsDict = new Dictionary(true);
            this.m_iconsDict = new Dictionary(true);
            this.m_toolTipDict = new Dictionary(true);
            super(new SoftBoxLayout(SoftBoxLayout.Y_AXIS, this.layoutGap, SoftBoxLayout.TOP));
        }
        public function get type():String{
            return (this.m_type);
        }
        public function get shelf():MarketScrollingList{
            return (this.m_shelf);
        }
        public function get topPanel():JPanel{
            return (this.m_topPanel);
        }
        public function get exclusive():Boolean{
            return (this.m_exclusive);
        }
        protected function get layoutGap():Number{
            return (-13);
        }
        public function set overrideTitle(_arg1:String):void{
            this.m_overrideTitle = _arg1;
            var _local2:TextFormat = new TextFormat();
            _local2.size = this.BIG_FONT_SIZE;
        }
        public function get overrideTitle():String{
            return (this.m_overrideTitle);
        }
        public function set exclusive(_arg1:Boolean):void{
            this.m_exclusive = _arg1;
            this.switchTabOpacity(this.m_exclusive);
        }
        public function init(_arg1:String, _arg2:Catalog, _arg3:String, _arg4:Boolean, _arg5:String):void{
            this.makeBackground();
            this.m_overrideTitle = _arg5;
            this.m_exclusive = _arg4;
            this.m_containerWidth = 760;
            this.m_catalog = _arg2;
            this.m_type = _arg1;
            this.m_itemName = _arg3;
            this.m_selectedTab = new AssetPane((new (Catalog.assetDict["selectedTab"])() as DisplayObject));
            ASwingHelper.setEasyBorder(this.m_selectedTab, 0, 10);
            this.m_subTabsNames = new Dictionary(true);
            this.m_topPanel = new JPanel(new BorderLayout());
            this.m_topLeftPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            this.m_tabsPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            this.m_centerPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            this.m_centerPanel.setPreferredHeight(Catalog.CARD_HEIGHT);
            this.m_tabNames = this.m_catalog.categoryNames;
            this.makeTopTabs(this.m_tabNames);
            this.append(ASwingHelper.verticalStrut(20));
            this.append(this.m_centerPanel);
            ASwingHelper.prepare(this);
            this.switchType(_arg1);
            this.goToItem(_arg3);
            this.addListeners();
            this.m_toolTip = new ItemCatalogToolTip();
            addChild(this.m_toolTip);
            this.m_toolTip.visible = false;
            this.m_isInitialized = true;
            this.addEventListener(MouseEvent.MOUSE_MOVE, this.catchMousePropagation, false, 0, true);
            this.addEventListener(MouseEvent.CLICK, this.catchMousePropagation, false, 0, true);
        }
        private function catchMousePropagation(_arg1:MouseEvent):void{
            Global.ui.turnOffHighlightedObject();
            _arg1.stopPropagation();
        }
        protected function makeShelf(_arg1:Array):MarketScrollingList{
            return (new MarketScrollingList(_arg1, MarketCellFactory, 0, 7, 1));
        }
        protected function switchTabOpacity(_arg1:Boolean):void{
            var _local2:String;
            var _local3:ToolTip;
            var _local4:JButton;
            var _local5:JPanel;
            for each (_local2 in this.m_tabNames) {
                _local3 = this.m_toolTipDict[_local2];
                _local4 = this.m_iconsDict[_local2].jbtn;
                _local5 = this.m_iconsDict[_local2].tab;
                if (_arg1){
                    _local5.removeAll();
                    _local5.append(ASwingHelper.verticalStrut(68));
                    _local4.visible = false;
                } else {
                    _local5.removeAll();
                    _local4.visible = true;
                    _local5.append(_local4);
                    if (_local2 == this.m_type){
                        _local5.append(this.m_selectedTab);
                        ASwingHelper.setEasyBorder(_local5, 0);
                    };
                };
            };
            ASwingHelper.prepare(this);
        }
        protected function renderTab(_arg1:String):void{
            var _local2:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var _local3:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER, -6);
            var _local4:JButton = new JButton();
            var _local5 = (("tab_icon_" + _arg1) + "_offstate");
            var _local6 = (("tab_icon_" + _arg1) + "_onstate");
            var _local7:DisplayObject = (new (Catalog.assetDict[_local5])() as DisplayObject);
            var _local8:DisplayObject = (new (Catalog.assetDict[_local6])() as DisplayObject);
            var _local9:SimpleButton = new SimpleButton(_local7, _local8, _local8, _local7);
            _local4.wrapSimpleButton(_local9);
            var _local10:Object = new Object();
            _local10.tab = _local3;
            _local10.image = _local7;
            _local10.button = _local9;
            _local10.jbtn = _local4;
            this.m_iconsDict[_arg1] = _local10;
            this.m_tabsDict[_local4] = _arg1;
            _local4.name = _arg1;
            _local3.append(_local4);
            ASwingHelper.setEasyBorder(_local3, 14);
            _local2.append(_local3);
            _local4.addActionListener(this.changeCategory, 0, true);
            ASwingHelper.prepare(_local2);
            this.m_tabsPanel.append(_local2);
            _local4.addEventListener(MouseEvent.ROLL_OVER, this.showToolTip, false, 0, true);
            _local4.addEventListener(MouseEvent.ROLL_OUT, this.hideToolTip, false, 0, true);
            if (((Global.isVisiting()) && (!((_arg1 == "business"))))){
                _local4.setEnabled(false);
            };
        }
        protected function makeTopTabs(_arg1:Array=null):void{
            var _local2:String;
            var _local3:JPanel;
            var _local4:JPanel;
            var _local5:JPanel;
            var _local6:JPanel;
            var _local7:String;
            for each (_local2 in _arg1) {
                this.renderTab(_local2);
            };
            this.m_catalog.addEventListener(MouseEvent.MOUSE_DOWN, this.onMarketDown, false, 0, true);
            this.m_catalog.addEventListener(MouseEvent.MOUSE_MOVE, this.onMarketDown, false, 0, true);
            this.m_catalog.addEventListener(MouseEvent.MOUSE_OVER, this.onMarketOver, false, 0, true);
            this.m_catalog.addEventListener(MouseEvent.MOUSE_OUT, this.onMarketOut, false, 0, true);
            _local3 = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.RIGHT);
            _local4 = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            this.m_closeButton = ASwingHelper.makeMarketCloseButton();
            this.m_closeButton.addActionListener(this.closeCatalog, 0, true);
            _local4.appendAll(this.m_closeButton, ASwingHelper.verticalStrut(10));
            _local3.append(_local4);
            _local3.append(ASwingHelper.horizontalStrut(15));
            _local5 = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.LEFT);
            _local5.append(ASwingHelper.horizontalStrut(15));
            this.m_topPanel.append(_local5, BorderLayout.WEST);
            this.m_topPanel.append(this.m_tabsPanel, BorderLayout.CENTER);
            this.m_topPanel.append(_local3, BorderLayout.EAST);
            this.append(this.m_topPanel);
            _local6 = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER, 10);
            if (this.m_overrideTitle != ""){
                _local7 = ZLoc.t("Dialogs", (this.m_overrideTitle + "_menu"));
            } else {
                _local7 = ZLoc.t("Dialogs", (this.m_type + "_menu"));
            };
            this.m_sectionTitle = ASwingHelper.makeTextField(_local7, EmbeddedArt.TITLE_FONT, 16, EmbeddedArt.blueTextColor);
            var _local8:TextFormat = new TextFormat();
            _local8.size = this.BIG_FONT_SIZE;
            TextFieldUtil.formatSmallCaps(this.m_sectionTitle.getTextField(), _local8);
            var _local9:AssetPane = new AssetPane((new (Catalog.assetDict["marketTitleRule"])() as DisplayObject));
            var _local10:AssetPane = new AssetPane((new (Catalog.assetDict["marketTitleRule"])() as DisplayObject));
            ASwingHelper.setEasyBorder(_local9, 18);
            ASwingHelper.setEasyBorder(_local10, 18);
            ASwingHelper.setEasyBorder(_local6, 3, 0, 2);
            LocaleHelper.localSetting.ASwingHelper_setEasyBorder(_local6, 5, 0, 4);
            _local6.appendAll(_local9, this.m_sectionTitle, _local10);
            this.append(_local6);
            this.switchTabOpacity(this.m_exclusive);
            ASwingHelper.prepare(this);
        }
        protected function onMarketDown(_arg1:MouseEvent):void{
            var _local6:GMDefault;
            var _local2:GameMode = Global.world.getTopGameMode();
            var _local3:Point = new Point(Global.ui.mouseX, Global.ui.mouseY);
            var _local4:Array = Global.ui.m_bottomUI.getObjectsUnderPoint(_local3);
            var _local5:int;
            _local5 = 0;
            while (_local5 < _local4.length) {
                if ((_local4[_local5] is Catalog.assetDict["bg"])){
                    if ((_local2 is GMDefault)){
                        _local6 = (_local2 as GMDefault);
                        _local6.turnOffObject();
                    };
                    _arg1.stopImmediatePropagation();
                    _arg1.stopPropagation();
                };
                _local5++;
            };
        }
        public function forceShowToolTip():void{
            if ((((this.m_type == "plot_contract")) || ((this.m_type == "ship_contract")))){
                return;
            };
            var _local1:Object = this.m_iconsDict[this.m_type];
            var _local2:JButton = _local1.jbtn;
            var _local3:String = this.m_tabsDict[_local2];
            var _local4:IntPoint = _local2.getGlobalLocation();
            var _local5:Number = _local4.x;
            var _local6:Point = Global.ui.m_bottomUI.localToGlobal(new Point(Global.ui.m_bottomUI.x, 0));
            if ((((this.m_type == "residence")) && (!((_local5 == (80 + _local6.x)))))){
                _local5 = (80 + _local6.x);
            };
            var _local7:Object = new Object();
            _local7.category = _local3;
            _local7.posX = _local5;
            dispatchEvent(new GenericObjectEvent(GenericObjectEvent.ROLLOVER_MARKET_CATEGORY, _local7));
        }
        protected function showToolTip(_arg1:MouseEvent):void{
            var _local2:JButton;
            var _local3:String;
            _local2 = (_arg1.currentTarget as JButton);
            _local3 = this.m_tabsDict[(_arg1.currentTarget as JButton)];
            var _local4:IntPoint = _local2.getGlobalLocation();
            var _local5:Number = _local4.x;
            var _local6:Object = new Object();
            _local6.category = _local3;
            _local6.posX = _local5;
            dispatchEvent(new GenericObjectEvent(GenericObjectEvent.ROLLOVER_MARKET_CATEGORY, _local6));
        }
        protected function hideToolTip(_arg1:MouseEvent):void{
            dispatchEvent(new Event("hidetooltip"));
        }
        protected function onMarketOver(_arg1:MouseEvent):void{
            UI.pushBlankCursor();
        }
        protected function onMarketOut(_arg1:MouseEvent):void{
            UI.popBlankCursor();
        }
        protected function onMenuClose(_arg1:MouseEvent):void{
            _arg1.stopImmediatePropagation();
            _arg1.stopPropagation();
        }
        protected function onCloseDown(_arg1:MouseEvent):void{
            _arg1.stopImmediatePropagation();
            _arg1.stopPropagation();
        }
        protected function closeOver(_arg1:MouseEvent):void{
            var _local2:JButton = (_arg1.target as JButton);
            var _local3:ColorMatrix = new ColorMatrix();
            _local3.adjustBrightness(1.5, 1.5, 1.5);
            _local3.adjustContrast(1.5, 1.5, 1.5);
            _local2.filters = [_local3.filter];
        }
        protected function closeOut(_arg1:MouseEvent):void{
            var _local2:JButton = (_arg1.target as JButton);
            _local2.filters = [];
        }
        public function updateElements():void{
            this.m_shelf.updateElements();
        }
        protected function changeCategory(_arg1:AWEvent):void{
            var _local2:String;
            var _local3:GameMode;
            if (!Global.isVisiting()){
                _local2 = _arg1.currentTarget.name;
                _local3 = Global.world.getTopGameMode();
                if ((_local3 is GMPlaceMapResource)){
                    Global.world.addGameMode(new GMPlay());
                };
                _arg1.stopImmediatePropagation();
                _arg1.stopPropagation();
                this.switchType(_local2);
            };
        }
        protected function makeBackground():void{
            var _local1:DisplayObject = (new (Catalog.assetDict["bg"])() as DisplayObject);
            var _local2:MarginBackground = new MarginBackground(_local1, new Insets(Catalog.TAB_HEIGHTOFFFSET));
            this.setBackgroundDecorator(_local2);
            this.setPreferredSize(new IntDimension(_local1.width, (_local1.height + Catalog.TAB_HEIGHTOFFFSET)));
        }
        protected function switchActiveTab(_arg1:String, _arg2:String):void{
            var _local4:SimpleButton;
            var _local5:DisplayObject;
            var _local6:JPanel;
            var _local7:Object;
            var _local8:SimpleButton;
            var _local9:DisplayObject;
            var _local10:JPanel;
            var _local3:Object = this.m_iconsDict[_arg2];
            if (_local3){
                _local4 = _local3.button;
                _local5 = _local3.image;
                _local6 = _local3.tab;
                _local4.upState = _local4.overState;
                _local6.append(this.m_selectedTab);
                ASwingHelper.setEasyBorder(_local6, 0);
            };
            if (((((!((_arg1 == _arg2))) && (!((_arg1 == "plot_contract"))))) && (!((_arg1 == "ship_contract"))))){
                _local7 = this.m_iconsDict[_arg1];
                if (_local7){
                    _local8 = _local7.button;
                    _local9 = _local7.image;
                    _local10 = _local7.tab;
                    _local8.upState = _local9;
                    _local10.remove(this.m_selectedTab);
                    ASwingHelper.setEasyBorder(_local10, 14);
                };
            };
        }
        public function switchType(_arg1:String):void{
            var _local4:JPanel;
            var _local2:int = this.m_tabIndices[_arg1];
            if (this.m_shelf){
                this.m_tabIndices[this.m_type] = this.m_shelf.currentItem;
            };
            if (((!((_arg1 == "plot_contract"))) && (!((_arg1 == "ship_contract"))))){
                this.switchActiveTab(this.m_type, _arg1);
            };
            this.m_type = _arg1;
            if (((this.m_shelf) && (this.m_shelf.hasEventListener(DataItemEvent.MARKET_BUY)))){
                this.m_shelf.removeListeners();
                this.m_shelf.removeEventListener(DataItemEvent.MARKET_BUY, this.onContainerClick);
                this.m_shelf.removeEventListener(Event.CHANGE, this.onShelfChange);
            };
            this.m_sectionTitle.setText(ZLoc.t("Dialogs", (this.m_type + "_menu")));
            var _local3:TextFormat = new TextFormat();
            _local3.size = this.BIG_FONT_SIZE;
            TextFieldUtil.formatSmallCaps(this.m_sectionTitle.getTextField(), _local3);
            this.m_centerPanel.removeAll();
            if (this.m_shelf != null){
                this.remove(this.m_shelfAP);
            };
            this.loadAvailableItems();
            if (((!((_arg1 == "vehicles"))) && (!((_arg1 == "wonders"))))){
                if (_arg1 == "new"){
                    this.m_newItems = Global.gameSettings.newItems;
                    this.m_newItems = this.removeOutOfScheduleItems(this.m_newItems);
                    this.m_shelf = this.makeShelf(this.m_newItems);
                } else {
                    this.m_shelf = this.makeShelf(this.m_items);
                };
                this.m_shelf.addEventListener(DataItemEvent.MARKET_BUY, this.onContainerClick, false, 0, true);
                this.m_shelf.addEventListener(Event.CHANGE, this.onShelfChange);
                ASwingHelper.prepare(this.m_shelf);
                this.m_centerPanel.append(this.m_shelf);
            } else {
                _local4 = new ComingSoonPanel(_arg1);
                this.m_centerPanel.append(_local4);
            };
            this.m_centerPanel.setPreferredHeight((Catalog.CARD_HEIGHT + 25));
            ASwingHelper.prepare(this.m_centerPanel);
            if (_local2 != -1){
                this.m_shelf.scrollToItemIndex(_local2);
            };
            this.trackPageView(this.m_type, this.m_shelf.currentPageIndex);
        }
        public function goToItem(_arg1:String):void{
            var _local2:Array = this.m_shelf.data;
            var _local3:int = CardUtil.indexOfByCardName(_local2, _arg1);
            if (_local3 != -1){
                this.m_shelf.scrollToItemIndex(_local3);
            };
        }
        public function shutdown():void{
            this.removeListeners();
            this.m_shelf = null;
            this.m_type = null;
            this.m_catalog = null;
        }
        public function get isInitialized():Boolean{
            return (this.m_isInitialized);
        }
        public function onTweenIn():void{
        }
        public function onTweenOut():void{
        }
        protected function removeListeners():void{
            var _local1:JPanel;
            for each (_local1 in this.m_tabsArray) {
                _local1.removeEventListener(MouseEvent.CLICK, this.changeCategory);
            };
            if (((stage) && (stage.hasEventListener(MouseEvent.CLICK)))){
                stage.removeEventListener(MouseEvent.CLICK, this.onStageClick);
            };
            if (((this.m_shelf) && (this.m_shelf.hasEventListener(DataItemEvent.MARKET_BUY)))){
                this.m_shelf.removeEventListener(DataItemEvent.MARKET_BUY, this.onContainerClick);
            };
            if (this.m_shelf){
                this.m_shelf.removeListeners();
            };
        }
        protected function addListeners():void{
            var me:* = null;
            if (stage != null){
                stage.addEventListener(MouseEvent.CLICK, this.onStageClick);
            } else {
                me = this;
                this.addEventListener(Event.ADDED_TO_STAGE, function (_arg1:Event):void{
                    me.removeEventListener(Event.ADDED_TO_STAGE, arguments.callee);
                    stage.addEventListener(MouseEvent.CLICK, onStageClick);
                });
            };
            this.m_shelf.addEventListener(DataItemEvent.MARKET_BUY, this.onContainerClick, false, 0, true);
        }
        protected function closeCatalog(_arg1:AWEvent):void{
            dispatchEvent(new Event("hidetooltip"));
            this.m_catalog.close();
            Global.marketSessionTracker.endSession();
        }
        protected function openMenu(_arg1:AWEvent):void{
            dispatchEvent(new UIEvent(UIEvent.OPEN_ACTION_MENU, "", true));
        }
        protected function onStageClick(_arg1:MouseEvent):void{
        }
        protected function onBackdropClick(_arg1:MouseEvent):void{
        }
        protected function onContainerClick(_arg1:DataItemEvent):void{
            Sounds.playFromSet(Sounds.SET_CLICK);
            var _local2:Item = _arg1.item;
            var _local3:Item = (_local2) ? _local2 : null;
            if (this.m_catalog.canBuy(_local3)){
                this.m_catalog.onBuy(_local3, _arg1.target);
            };
        }
        protected function onShelfChange(_arg1:Event):void{
            this.trackPageView(this.m_type, this.m_shelf.currentPageIndex);
        }
        protected function trackPageView(_arg1:String, _arg2:int):void{
            StatsManager.count("market_views", _arg1, ("page" + (_arg2 + 1).toString()));
        }
        protected function loadAvailableItems():void{
            var _local2:String;
            var _local3:Array;
            var _local1:Array = Global.gameSettings.getBuyableCategories(this.m_type);
            this.m_items = Global.gameSettings.getCurrentBuyableItems(this.m_type);
            this.m_newItems = Global.gameSettings.newItems;
            if (((Global.isVisiting()) && ((this.m_type == "business")))){
                this.m_items = Global.franchiseManager.eligibleFranchises;
            };
            if (!this.m_exclusive){
                for each (_local2 in _local1) {
                    _local3 = Global.gameSettings.getCurrentBuyableItems(_local2);
                    this.m_items = this.m_items.concat(_local3);
                };
            };
            this.m_items = this.removeOutOfScheduleItems(this.m_items);
        }
        public function refreshShelf():void{
        }
        private function removeOutOfScheduleItems(_arg1:Array):Array{
            var items:* = _arg1;
            return (items.filter(function (_arg1:Object, _arg2:int, _arg3:Array=null):Object{
                if (((!(_arg1.startDate)) && (!(_arg1.endDate)))){
                    return (true);
                };
                var _local4:* = new Date().getTime();
                if (((((((_arg1.startDate) && (_arg1.endDate))) && ((_local4 > _arg1.startDate)))) && ((_local4 < _arg1.endDate)))){
                    return (true);
                };
                return (false);
            }));
        }

    }
}//package Display.MarketUI 
